﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ManiaDown.SayoApi
{
    class BeatmapListQuery
    {
        /// <summary>
        /// 本次请求返回结果数
        /// </summary>
        public int Limit { get; set; } = 25;
        /// <summary>
        /// 第一条结果位置
        /// </summary>
        public int Offset { get; set; } = 0;
        /// <summary>
        /// 请求类型
        /// </summary>
        public ActionTypes Type { get; set; } = ActionTypes.Search;
        /// <summary>
        /// 关键字
        /// </summary>
        public string Keyword { get; set; } = string.Empty;
        /// <summary>
        /// 关键字精准匹配类型
        /// </summary>
        public SubTypes SubType { get; set; } = SubTypes.Default;
        /// <summary>
        /// 游戏模式
        /// </summary>
        public Modes Mode { get; set; } = Modes.Default;
        /// <summary>
        /// 铺面状态
        /// </summary>
        public MapStatus Status { get; set; } = MapStatus.Default;
        /// <summary>
        /// 铺面风格
        /// </summary>
        public MapGenre Genre { get; set; } = MapGenre.Default;
        /// <summary>
        /// 铺面语言
        /// </summary>
        public MapLaunguage Launguage { get; set; } = MapLaunguage.Default;
        /// <summary>
        /// 高级搜索条件
        /// </summary>
        public AdvanceLimit AdvanceLimit { get; set; } = AdvanceLimit.Default;

        public BeatmapListQuery() { }
        public BeatmapListQuery(Modes modes, MapStatus status, AdvanceLimit advance)
        {
            Mode = modes;
            Status = status;
            AdvanceLimit = advance;
        }

        public override string ToString() =>
            $"0={Limit}&1={Offset}&2={(uint)Type}&3={Keyword}&4={(uint)SubType}" +
            $"&5={(uint)Mode}&6={(uint)Status}&7={(uint)Genre}&8={(uint)Launguage}&9={AdvanceLimit}";
        

        private static BeatmapListQuery Count { get; set; }

        public static void SetQueryLimit(Modes modes, MapStatus status, AdvanceLimit advance)
        {
            Count = new BeatmapListQuery(modes, status, advance);
        }
        public static void SetQueryLimit(BeatmapListQuery query)
        {
            Count = query;
        }

        ///TODO: 访问级别屏蔽

        /// <summary>
        /// 按照预先设置的条件，获取下一页数据查询串
        /// </summary>
        /// <returns></returns>
        public static BeatmapListQuery Next()
        {
            if(Count==null)
            {
                throw new InvalidOperationException();
            }
            Count.Offset += Count.Limit;
            return Count;
        }
        /// <summary>
        /// 按照预先设置的条件，获取上一页数据查询串
        /// </summary>
        /// <returns></returns>
        public static BeatmapListQuery Prev()
        {
            if(Count == null)
            {
                throw new InvalidOperationException();
            }
            Count.Offset -= Count.Limit;
            if (Count.Offset < 0) Count.Offset = 0;
            return Count;
        }
    }

    struct Range
    {
        public double Left { get; set; }
        public double Right { get; set; }
        public Range(double left,double right)
        {
            Left = left;
            Right = right;
        }
        public override string ToString() => $"{Left}~{Right}";

    }

    /// <summary>
    /// 高级搜索条件
    /// </summary>
    class AdvanceLimit
    {
        public static AdvanceLimit Default { get; } = new AdvanceLimit();

        public Range Star { get; set; } = new Range(0, 10);
        public Range AR { get; set; } = new Range(0, 10);
        public Range OD { get; set; } = new Range(0, 10);
        public Range CS { get; set; } = new Range(0, 10);
        public Range HP { get; set; } = new Range(0, 10);
        public Range Lenth { get; set; } = new Range(0, 999);
        public Range BPM { get; set; } = new Range(0, 9999);

        public AdvanceLimit() { }
        public AdvanceLimit(Range star) { Star = star; }

        public override string ToString() =>
            $"star:{Star},AR:{AR},OD:{OD},CS:{CS},HP:{HP},lenth:{Lenth},BPM:{BPM},end";
        
    }

    /// <summary>
    /// 请求类型
    /// </summary>
    enum ActionTypes : uint
    {
        /// <summary>
        /// 热歌
        /// </summary>
        Hot = 1,
        /// <summary>
        /// 新歌
        /// </summary>
        New = 2,
        /// <summary>
        /// 曲包
        /// </summary>
        Packs = 3,
        /// <summary>
        /// 搜索
        /// </summary>
        Search = 4
    }

    /// <summary>
    /// 精确匹配关键字
    /// </summary>
    enum SubTypes : uint
    {
        /// <summary>
        /// 标题
        /// </summary>
        Title = 1,
        /// <summary>
        /// 艺术家
        /// </summary>
        Artist = 2,
        /// <summary>
        /// 作图者
        /// </summary>
        Creator = 4,
        /// <summary>
        /// 版本
        /// </summary>
        Version = 8,
        /// <summary>
        /// 标签
        /// </summary>
        Tags = 16,
        /// <summary>
        /// 来源
        /// </summary>
        Source = 32,
        Default = 0xffffffff
    }

    /// <summary>
    /// 匹配铺面模式
    /// </summary>
    enum Modes : uint
    {
        /// <summary>
        /// STD模式
        /// </summary>
        Std = 1,
        /// <summary>
        /// 太鼓模式
        /// </summary>
        Taiko = 2,
        /// <summary>
        /// 接翔模式
        /// </summary>
        Ctb = 4,
        /// <summary>
        /// 钢琴模式
        /// </summary>
        Mania = 5,
        Default = 0xffffffff
    }

    /// <summary>
    /// 匹配铺面状态
    /// </summary>
    enum MapStatus : uint
    {
        /// <summary>
        /// Ranked或Approved状态
        /// </summary>
        Ranked = 1,
        /// <summary>
        /// Qualified状态
        /// </summary>
        Qualified = 2,
        /// <summary>
        /// Loved状态
        /// </summary>
        Loved = 4,
        /// <summary>
        /// Pending或Work In Progress(WIP)状态
        /// </summary>
        Pending = 8,
        /// <summary>
        /// Graveyard状态
        /// </summary>
        Graveyard = 16,
        Default = 0xffffffff
    }

    /// <summary>
    /// 匹配铺面风格
    /// </summary>
    enum MapGenre : uint
    {
        /// <summary>
        /// 不限风格
        /// </summary>
        Any = 1,
        /// <summary>
        /// 未指定风格
        /// </summary>
        Undefined = 2,
        /// <summary>
        /// 电子游戏
        /// </summary>
        Game = 4,
        /// <summary>
        /// 动漫
        /// </summary>
        Anime = 8,
        /// <summary>
        /// 摇滚
        /// </summary>
        Rock = 16,
        /// <summary>
        /// 流行
        /// </summary>
        Pop = 32,
        /// <summary>
        /// 其它
        /// </summary>
        Other = 64,
        /// <summary>
        /// 新奇
        /// </summary>
        Curious = 128,
        /// <summary>
        /// 嘻哈
        /// </summary>
        HipHop = 256,
        /// <summary>
        /// 电子
        /// </summary>
        Electronic = 1024,
        Default = 0xffffffff
    }

    /// <summary>
    /// 指定铺面语言
    /// </summary>
    enum MapLaunguage : uint
    {
        /// <summary>
        /// 不限语言
        /// </summary>
        Any = 1,
        /// <summary>
        /// 其它
        /// </summary>
        Other = 2,
        /// <summary>
        /// 英语
        /// </summary>
        English = 4,
        /// <summary>
        /// 日语
        /// </summary>
        Japanese = 8,
        /// <summary>
        /// 汉语
        /// </summary>
        Chinese = 16,
        /// <summary>
        /// 器乐
        /// </summary>
        Instrumental = 32,
        /// <summary>
        /// 韩语
        /// </summary>
        Korean = 64,
        /// <summary>
        /// 法语
        /// </summary>
        French = 128,
        /// <summary>
        /// 德语
        /// </summary>
        German = 256,
        /// <summary>
        /// 瑞典语
        /// </summary>
        Swedish = 512,
        /// <summary>
        /// 西班牙语
        /// </summary>
        Spanish = 1024,
        /// <summary>
        /// 意大利语
        /// </summary>
        Italian = 2048,
        Default = 0xffffffff
    }
}
